﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.Text.RegularExpressions;

namespace bulkuploader_gui
{
    /// <summary>
    /// Interaction logic for Window1.xaml
    /// </summary>
    public partial class addNewEntity : Window
    {
        private string selectedApp = null;
        public addNewEntity(string appNamePrm)
        {
            InitializeComponent();
            selectedApp = appNamePrm;
        }


        private void showFileBrowser(object sender, RoutedEventArgs e)
        {
           
            System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
            
            //show dialog box
            System.Windows.Forms.DialogResult result = dlg.ShowDialog();

            // OK button was pressed.
            if (result == System.Windows.Forms.DialogResult.OK)
            {
                //textBox2.Text = dlg.SelectedPath;
            }
        }

        private void addButton_Click(object sender, RoutedEventArgs e)
        {
            Regex pattern = new Regex("^[a-zA-Z0-9_]+$");
            
            if(pattern.IsMatch(textBox5.Text))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load("appData.xml");

                if (doc.SelectSingleNode("//entity[@id='" + textBox5.Text + "']") == null)
                {

                    XmlElement entity = doc.CreateElement("entity");
                    XmlElement kind = doc.CreateElement("kind");

                    kind.InnerText = textBox5.Text;

                    entity.AppendChild(kind);
                    entity.SetAttribute("id", kind.InnerText);
                    doc.SelectSingleNode("/apps/app[@name='" + selectedApp + "']").AppendChild(entity);


                    doc.Save("appData.xml");
                    this.Close();
                }
                else
                    System.Media.SystemSounds.Beep.Play();
                
            }
            else
                System.Media.SystemSounds.Beep.Play();
        }
    }
}
